import vue from 'vue'
import VueRouter from "vue-router";
import homeVue from "@/pages/home/home.vue";
import loginVue from "@/pages/login/login.vue";
import registerVue from "@/pages/register/register.vue";
import searchVue from "@/pages/search/search.vue";

vue.use(VueRouter)

//更改push方法，使得多次访问同一个参数路由不报错
let originPush = VueRouter.prototype.push; //备份原型对象上的方法
VueRouter.prototype.push = function push(location, resolve, reject) {
    //对原型对象上的方法重新定义，将自定义函数作为新的push方法
    if (resolve & reject) { //是否传递了 resolve 和 reject 参数?
        originPush.call(this, location, resolve, reject) //调用原来的 push 方法，并将传递的参数传递给它
    } else { //在调用 push 方法时没有提供回调函数
        originPush.call(this, location, () => { }, () => { }) //调用原来的 push 方法，并传递两个空的箭头函数作为回调函数,避免错误
    }
}
//重写replace
let originReplace = VueRouter.prototype.replace; //备份原型对象上的方法
VueRouter.prototype.replace = function replace(location, resolve, reject) {
    if (resolve & reject) {
        originReplace.call(this, location, resolve, reject)
    } else {
        originReplace.call(this, location, () => { }, () => { })
    }
}

export default new VueRouter({
    routes: [
        {
            path: '/home',
            component: homeVue,
            meta: { showHeaderFooter: true }
        },
        {
            path: '/login',
            component: loginVue,
            meta: { showHeaderFooter: false }
        },
        {
            path: '/register',
            component: registerVue,
            meta: { showHeaderFooter: false }
        },
        {
            path: '/search/:keyword?',
            component: searchVue,
            name: 'search',
            meta: { showHeaderFooter: true }
        },
        {
            path: '*',
            redirect: '/home'  //重定向
        }
    ]
})